package com.dk.controller;

import com.dk.pojo.Menu;
import com.dk.pojo.TPerson;
import com.dk.service.MenuService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("menu")
public class MenuController {

    @DubboReference
    private MenuService menuService;

    @RequestMapping("treeview")
    public String treeview() {

        return "treeview";
    }

    @RequestMapping("toShow")
    public String show() {

        return "menu/show";
    }

    @RequestMapping("getInfo")
    @ResponseBody
    public List<Map<String,Object>> getInfo() {

        Subject subject = SecurityUtils.getSubject();

        TPerson tPerson =(TPerson) subject.getPrincipal();

        List<Map<String,Object>> list = menuService.getInfo(tPerson.getPid());

        return list;
    }

    @RequestMapping("getMenuZtree")
    @ResponseBody
    public List<Map<String,Object>> getMenuZtree() {

        List<Map<String,Object>> list = menuService.getMenuZtree();

        return list;
    }

    @RequestMapping("toAdd")
    public String toAdd() {

        return "menu/add";
    }

    @RequestMapping("addInfo")
    @ResponseBody
    public boolean add(Menu menu) {

        return menuService.addInfo(menu);
    }

    @RequestMapping("deleteById")
    @ResponseBody
    public boolean deleteById(Integer id) {

        return menuService.deleteInfo(id);
    }

    @RequestMapping("toUpdate")
    public String toUpdate(Integer id, Model model) {

        Menu menu = menuService.selectById(id);

        model.addAttribute("menu",menu);

        return "menu/update";
    }

    @RequestMapping("updateMenuInfo")
    @ResponseBody
    public boolean updateMenuInfo(Menu menu) {

        return menuService.updateMenuInfo(menu);
    }

    @RequestMapping("getMenuZtreeChecked/{roleId}")
    @ResponseBody
    public List<Map<String,Object>> getMenuZtreeChecked(@PathVariable("roleId") Integer roleId) {

        List<Map<String,Object>> list = menuService.getMenuZtreeChecked(roleId);

        return list;
    }
}
